

/*
 * @Author       : DonnieLiang
 * @Date         : 2022-09-02 
 * @Version      : 1.0
 * @Description  : timer_app.c
 */

#include <stdio.h>
#include <string.h>

#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>   //sleep

#include <ctype.h>

//#include <memory.h> /*使用memcpy所需的头文件*/
//#include <stdlib.h>
//#include "app_types.h"
#include "gui_logist.h"
#include "iotv30_config.h"

//#include "ISR.h"
//#include "PLC.h"
//#include "TaskProcess.h"
//#include  "CCT_Logic.h"


#include "f133_drv_config.h"

#include  "TimeBase.h"

#include "iotv30_app.h"


#include "proto_plat.h"
#include "rtc_calendar.h"
#include "timer_app.h"



void timeout1(int sig)
{
	time_t times=0;
	 printf("timeout1() tick time:%ld\n", time(&times) );	


}


void settime_alarm(void)
{
    static struct itimerval tick;
    
    signal(SIGALRM, timeout1);
    memset(&tick, 0, sizeof(tick));
 
     //第一次调用时间
    tick.it_value.tv_sec = 10;
    tick.it_value.tv_usec = 0;
	
    //第一次调用后，每次调用间隔时间
    tick.it_interval.tv_sec = 5;
    tick.it_interval.tv_usec = 0;

  // tick.it_interval = tick.it_value; 


	
    if(setitimer(ITIMER_REAL, &tick, NULL) < 0)
    {
        printf("Set timer failed!\n");
    }


    
}



void Timer_Exam1(void) { 

	 settime_alarm();
	
    //Timer_Exam2( ) ;

}


// 周期1ms
void timeout_isr(int sig)
{

static uint8_t   sec_cnt = 0;
static uint8_t   sec_10ms_cnt = 0;
static uint16_t  sec_ms_cnt = 0;

    APP_SysTick_ISR( );

    sec_10ms_cnt++;

    if( sec_10ms_cnt>9)
    {//周期 10ms


//新的路灯协议---IoTv3.0---protobuf协议  
        cm_timer_callback_10ms(  );

        sec_10ms_cnt = 0;

    }



//	time_t times=0;
//	printf("timeout_isr:%ld\n", time(&times) );	

    sec_ms_cnt++;

    if( sec_ms_cnt>999)
    {//周期 1sec

#if  DEBUG_PRINT_ENABLE
 //logout( "timeout_isr() \r\n")  ;
#endif
        u8SecIntFlag = 1 ;
        sec_ms_cnt = 0 ;


        RTC_Process(  );
        
//		PLC_Logist_Tick_Second( );			
 //       CCT_Logist_Tick( );

          iotv30_TCP_link_led_tick(  );
          gui_logist_tick_sec( );   //debug----2024-03-27---ver3.2

          plat_report_attr_tick_sec( );

        if( sec_cnt>1)
        {//周期 2sec
            sec_cnt = 0 ;

            //App_TaskSlowPro_Sec(  );          
           //iot_Node_Test_tick_min( );
        }


    }


}




int Set_Timer_Period(int sec,int usec,__sighandler_t fun)
{
    static struct itimerval tick;
    
    signal(SIGALRM, fun);

    memset(&tick, 0, sizeof(tick));
 
     //第一次调用时间
    tick.it_value.tv_sec  = sec;
    tick.it_value.tv_usec = usec;
	
    //第一次调用后，每次调用间隔时间
    tick.it_interval = tick.it_value; 


	
    if( setitimer(ITIMER_REAL, &tick, NULL) < 0 )
    {
        printf("Set timer failed!\n");
        return -1;
    }else{

       return 0;    
    }

    
}



void Timer_App_Init(void)
{
    TimeBase_Init( );
}


void Timer_App_Config_1ms(void)
{
    //TimeBase_Init( );
    //配置1ms定时器
      Set_Timer_Period( 0,1000,timeout_isr );//100ms   
  //   Set_Timer_Period( 0,100000,timeout_isr );//100ms

}



//简单打印信息，定时器触发函数
void print_info(int signo){
    printf("timer fired = %d\n", signo );
}

void init_sigaction(){
    struct sigaction act;
    act.sa_handler = print_info;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask); 
    sigaction(SIGPROF,&act,NULL); //设置信号 SIGPROF 的处理函数为 print_info

}


void init_time() { 
    struct itimerval value; 
    value.it_value.tv_sec=2; //定时器启动后，每隔2秒将执行相应的函数
    value.it_value.tv_usec=0; 
    value.it_interval=value.it_value; 
    setitimer(ITIMER_PROF,&value,NULL); //初始化 timer，到期发送 SIGPROF 信号
} 



void Timer_Exam2(void) { 

    init_sigaction(); 
    init_time(); 

}





















